<?php

/**
* get how many selected drops
*
*/
$dropTimes = sizeof($_POST["dropBox"]);

//usrDebug("dropTimes",$dropTimes);

/**
* Reupdate me please since
* This code already built by MenuAccelerator
*
*/
//list all records name
$lstArray = array("trDateEnt",
                  "dueDateEnt",
                  "whoseEnt",
                  "currencyEnt",
                  "rateEnt",
                  "remainEnt",
                  "paymentEnt");


//shift it for many times
foreach($lstArray as $list){

  for($i = $dropTimes ; $i > 0 ; $i--){

    /**
    * Since $_POST["dropBox"] index is using array numbering
    * then we need to reduce the droppoint by 1.
    * E.g: $dropTimes = 3 => $_POST["dropBox"][3] will result Error
    *      since the dropTimes is trying to point to 3rd element
    *      of $_POST["dropBox"], which array wise is at [2] meaning 3rd element
    *
    */
    $dropPoint = $_POST["dropBox"][($i-1)];

    /**
    * slice it! so we could have the buffer's tail
    *
    */
    $bufTail   = array_slice($_POST[$list], $dropPoint);

    //var_dump($bufTail);

    //replace it
    array_splice($_POST[$list],               /* The original array or dummy */
                ($dropPoint - ARRAY_NUMBER),  /* Calculate the cursor, since it comes with array numbering */
                sizeof($bufTail),             /* The sizeof array concat with */
                $bufTail);                    /* The array we concat with */

    //var_dump($_POST[$list]);
  }
}
unset($list);

$newNum = $_SESSION["init_sub"] - $dropTimes;

if($newNum >= 0){

  $_SESSION["init_sub"] = $newNum;
}else{

  $_SESSION["init_sub"] = 0;
}

/**
 * fetch the detail
 * and foreach fetched data
 * concat at the end of current rows of data
 */
for($iRow = 0; $iRow < $_SESSION["init_sub"] ; $iRow++){

  switch($_POST["typeEnt"]){

    case _CASH_BANK_IN:

      switch($_POST["txEnt"]){

        case _CASH_BANK_DP_NAME:

          $CLFetchers->fetchOutstandingRemain($Db,
                                              $_GET["whoseCode"],
                                              $curPeriode,
                                              $_POST["invoice"][$iRow],
                                              $srcDbMutIvcCustomer,
                                              $nRemain,
                                              $resRemain);

          $CLFetchers->fetchSourceHeader($Db,
                                         $_POST["invoice"][$iRow],
                                         $srcDbSourceHdr,
                                         $nHeader,
                                         $resHeader);

          /**
           * process the gain or loss
           */
          $todayCurs   = $_GET["currency"];
          $todayRate   = $_GET["rate"];

          $thatDayCurs = $resHeader["CURRENCY"][0];
          $thatDayRate = $resHeader["RATE"][0];

          if($thatDayCurs == $todayCurs){

            $gainLoss = $todayRate - $thatDayRate;
          }else{

            /* Pending */
            $gainLoss = 0;
          }

          /**
           * get current nRows of input data
           * it could be any valid input name
           */
          $curIndex = sizeof($_POST["trDateEnt"]);

          $_POST["notaSrcEnt"][$curIndex]  = $resHeader["NOTA_SRC"][0];

          $_POST["currencyEnt"][$curIndex] = $resHeader["CURRENCY"][0];

          $_POST["rateEnt"][$curIndex]     = $resHeader["RATE"][0];

          $_POST["trDateEnt"][$curIndex]   = date("d-m-Y",
                                                  strtotime($resRemain["TR_DATE"][0]));
          $_POST["dueDateEnt"][$curIndex]  = date("d-m-Y",
                                                  strtotime($resRemain["DUE_DATE"][0]));

          $_POST["remainEnt"][$curIndex]   = $resRemain["REMAIN"][0];

          $_POST["receiveEnt"][$curIndex]  = $resRemain["REMAIN"][0];
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Tx");
      }
      break;

    case _CASH_BANK_OUT:

      switch($_POST["txEnt"]){

        case _CASH_BANK_DP_NAME:

          $CLFetchers->fetchOutstandingRemain($Db,
                                              $_GET["whoseCode"],
                                              $curPeriode,
                                              $_POST["invoice"][$iRow],
                                              $srcDbMutIvcSupplier,
                                              $nRemain,
                                              $resRemain);

          $CLFetchers->fetchSourceHeader($Db,
                                         $_POST["invoice"][$iRow],
                                         $srcDbSourceHdr,
                                         $nHeader,
                                         $resHeader);

          /**
           * @todo what if ref has different curs? and is CCL DP applied?
           *
           * process the gain or loss
           */
          $todayCurs   = $_GET["currency"];
          $todayRate   = $_GET["rate"];

          $thatDayCurs = $resHeader["CURRENCY"][0];
          $thatDayRate = $resHeader["RATE"][0];

          if($thatDayCurs == $todayCurs){

            $gainLoss = $thatDayRate - $todayRate;
          }else{

            /* Pending */
            $gainLoss = 0;
          }

          /**
           * get current nRows of input data
           * it could be any valid input name
           */
          $curIndex = sizeof($_POST["trDateEnt"]);

          $_POST["notaSrcEnt"][$curIndex]  = $resHeader["NOTA_SRC"][0];

          $_POST["currencyEnt"][$curIndex] = $resHeader["CURRENCY"][0];

          $_POST["rateEnt"][$curIndex]     = $resHeader["RATE"][0];

          $_POST["trDateEnt"][$curIndex]   = date("d-m-Y",
                                                  strtotime($resRemain["TR_DATE"][0]));
          $_POST["dueDateEnt"][$curIndex]  = date("d-m-Y",
                                                  strtotime($resRemain["DUE_DATE"][0]));

          $_POST["remainEnt"][$curIndex]   = $resRemain["REMAIN"][0];

          $_POST["paymentEnt"][$curIndex]  = $resRemain["REMAIN"][0];
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Tx");
      }
      break;

    default:
      $Misc->usrDieJs("Error: Unknown Type");
  }
}
?>